import performance from './performance/index.js';
import error from './error/index.js';
import behavior from './behavior/index.js';
import { lazyReportBatch } from './report.js';
import {setConfig} from './config.js';

window.__webEyeSDK__ = {
    version: "0.0.1",
}

// 针对vue项目的错误捕获 安装插件
// app.config.errorHandler：https://cn.vuejs.org/api/application.html#app-config-errorhandler
// 定义插件：https://cn.vuejs.org/guide/reusability/plugins.html#introduction
export function install(Vue, options) {
    if(__webEyeSDK__.vue) return;
    __webEyeSDK__.vue = true;
    setConfig(options);
    const handler = Vue.config.errorHandler;
    // vue项目中通过Vue.config.errorHandler来捕获错误
    Vue.config.errorHandler = function(err, vm, info) {
        // todo:上报具体的错误信息
        const reportData = {
            info,
            error:err.stack,
            subType:"vue",
            type:'error',
            startTime:window.performance.now(),
            pageURL:window.location.href
        }
        // console.log('vue error', reportData);
        lazyReportBatch(reportData);
        if(handler) {
            handler.call(this, err, vm, info);
        }
    }
}

// 针对react项目的错误捕获
// componentdidcatch:https://react.docschina.org/reference/react/Component#componentdidcatch
export function errorBoundary(err,info) {
    if(__webEyeSDK__.react) return;
    __webEyeSDK__.react = true;
    // todo:上报具体的错误信息
    const reportData = {
        error:err?.stack,
        info,
        subType:"react",
        type:'error',
        startTime:window.performance.now(),
        pageURL:window.location.href
    }
    lazyReportBatch(reportData);

}

export function init(options) {
    setConfig(options);
    // performance();
    // error();
    behavior();
}


export default {
    install,
    errorBoundary,
    performance,
    error,
    behavior,
    init
}